package com.example.demo.controller;

import com.example.demo.entity.ExamPaper;
import com.example.demo.entity.ExamQuestions;
import com.example.demo.service.impl.ExamPaperServiceImpl;
import com.example.demo.service.impl.ExamQuestionsServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/paper")
@RequiredArgsConstructor
@Component
public class ExamPaperController {
    private final ExamPaperServiceImpl examPaperService;
    private final ExamQuestionsServiceImpl examQuestionsService;
    @PostMapping("/addpaper")
    public void addPaper(@RequestBody ExamPaper examPaper) {
        ExamPaper examPaper1=examPaperService.selectById(examPaper.getId());
        if(examPaper1!=null){
            examPaperService.updateById(examPaper);
        }else{
            examPaperService.save(examPaper);
        }

    }
    @GetMapping("/selectall")
    public List<ExamPaper> selectAll() {
        List<ExamPaper> list = examPaperService.selectAll();
        return list;
    }
    @GetMapping("/paperdetail")
    public List<ExamQuestions> selectDetail(Integer id) {
        ExamPaper examPaper = examPaperService.selectById(id);
        String[] idList=examPaper.getQuestionid().split(",");
        List<ExamQuestions> list=new ArrayList<>();
        for (int i = 0; i < idList.length; i++) {
            ExamQuestions examQuestions=examQuestionsService.selectById(idList[i]);
            list.add(examQuestions);
        }
        return list;
    }
    @PostMapping("/updatepaper")
    public void updatePaper(@RequestBody ExamPaper examPaper) {
        examPaperService.updateById(examPaper);
    }
    @PostMapping("/deletepaper")
    public void deletePaper(@RequestBody ExamPaper examPaper) {

        String[] idArr=examPaper.getQuestionid().split(",");
        for(int i=0;i< idArr.length;i++){
            System.out.println(idArr[i]);
            examPaperService.deleteById(Integer.parseInt(idArr[i]));
        }
    }

}
